<?php

class Admin_CatalogCategoryController extends My_Controller_ActionAdmin {

    public function init() {
        parent::init();
        $this->view->menuSelected = 'catalog';
        $this->view->submenuSelected = 'catalog-category-' . $this->_request->getActionName();
    }

    public function indexAction() {
        $daoCategory = new Application_Model_DAO_CatalogCategory();
        $cats = $daoCategory->getFullCategories();
        $list = array();
        foreach ($cats as $cat) {
            $list[] = $cat;
            if (!empty($cat['categories'])) {
                $list = array_merge($list, $cat['categories']);
            }
        }
        $this->view->list = $list;
    }

    public function deleteAction() {
        $code = $this->_getParam('id');
        $form = new Admin_Form_Confirmation();

        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            if (!$this->_request->isPost()) {
                $form->getElement('id')->setValue($code);
            }
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        if (!$form->isConfirmed()) {
            return;
        }
        $code = $form->getElement('id')->getValue();
        $daoCatalogCategory = new Application_Model_DAO_CatalogCategory();
        $result = $daoCatalogCategory->delete($code, 'code');
        if ($result == 'deleted') {
            $this->addSuccess($this->translate('XXX_ADMIN_CATALOG_CATEGORY_DELETE_SUCCESS'));
        } elseif ($result == 'flagged') {
            $this->addSuccess($this->translate('XXX_ADMIN_CATALOG_CATEGORY_DELETE_BY_FLAG_SUCCESS'));
        } else {
            $this->addWarning($this->translate('XXX_ADMIN_CATALOG_CATEGORY_NOTHING_TO_DELETE_SUCCESS'));
        }
        $url = $this->url(array('controller' => 'catalog-category', 'action' => 'index'));
        $this->_redirect($url);
    }

    public function addAction() {
        $code = $this->_getParam('id', 'root');
        $isRoot = false;
        $category = null;
        $daoCategory = new Application_Model_DAO_CatalogCategory();
        if ($code == 'root') {
            $isRoot = true;
        } else {
            $category = $daoCategory->getByCode($code);
            if (empty($category)) {
                $this->addError($this->translate('XXX_ADMIN_CATALOG_CATEGORY_NOT_EXISTS'));
                $url = $this->url(array('controller' => 'offer-category'));
                $this->_redirect($url);
            }
        }
        $form = new Admin_Form_CatalogCategory_Add($category);

        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        $data = $form->getValues();

        if (!$isRoot) {
            $data['parent_id'] = $category['catalog_category_id'];
        } else {
        }
        $daoCategory->addNew($data);


        $url = $this->url(array('controller' => 'catalog-category'));
        $this->_redirect($url);
    }

}

